﻿using Newtonsoft.Json;
using Xwltz.Framework.Helper;

namespace Xwltz.Framework.Tool
{
    /// <summary>
    /// JSON适配类型
    /// </summary>
    public enum JsonContentType
    {
        Model,
        Data,
        Grid,
        Tree,
        Cmd
    }

    /// <summary>
    /// JSON适配器
    /// </summary>
    public class JsonAdapter
    {
        private const string SuccessMsg = "操作成功";
        private const string ErrorMsg = "操作失败";
        
        /// <summary>
        /// 返回详情
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public string Detail(object obj)
        {
            return obj.ToJson();
        }

        /// <summary>
        /// 返回数据列表
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public string Data(object obj)
        {
            var json = JsonConvert.SerializeObject(obj);
            return string.Concat("{\"data\":", json, "}");
        }

        /// <summary>
        /// 返回数据列表[LayUI][EasyUI]
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="count"></param>
        /// <param name="layui"></param>
        /// <returns></returns>
        public string Grid(object obj, int count, bool layui = false)
        {
            if (layui)
            {
                var text = JsonConvert.SerializeObject(obj);
                return string.Concat("{\"code\":", 0, ",\"msg\":", "\"获取成功\"", ",\"data\":", text, ",\"count\":", count, "}");
            }
            else
            {
                var text = JsonConvert.SerializeObject(obj);
                return string.Concat("{\"rows\":", text, ",\"total\":", count, "}");
            }
        }

        /// <summary>
        /// 返回DataTables数据列表
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="count"></param>
        /// <param name="draw"></param>
        /// <returns></returns>
        public string DtGrid(object obj, int count, string draw)
        {
            var text = JsonConvert.SerializeObject(obj);
            return string.Concat("{\"draw\":", draw, ",\"data\":", text, ",\"recordsTotal\":", count, ",\"recordsFiltered\":", count, "}");
        }

        /// <summary>
        /// 返回EasyUI TreeGrid列表
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public string TreeGrid(object obj, int count)
        {
            var text = JsonConvert.SerializeObject(obj);
            return string.Concat("{\"rows\":", text, ",\"total\":", count, "}");
        }

        /// <summary>
        /// 执行自定义返回命令
        /// </summary>
        /// <param name="success"></param>
        /// <param name="message"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public string Command(bool success, string message, params string[] args)
        {
            if (success)
            {
                message = string.IsNullOrEmpty(message) ? SuccessMsg : message;
            }
            else
            {
                message = string.IsNullOrEmpty(message) ? ErrorMsg : message;
            }
            return JsonConvert.SerializeObject(new { success, message });
        }

        /// <summary>
        /// 根据字符串返回模型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public T GetObject<T>(string json)
        {
            return JsonConvert.DeserializeObject<T>(json);
        }
    }
}